namespace Core.Reflection;

public static class TypeProvider
{
    private static bool IsRecord(this Type objectType) =>
        objectType.GetMethod("<Clone>$") != null ||
        ((TypeInfo)objectType)
        .DeclaredProperties.FirstOrDefault(x => x.Name == "EqualityContract")?
        .GetMethod?.GetCustomAttribute(typeof(CompilerGeneratedAttribute)) != null;

    public static Type? GetTypeFromAnyReferencingAssembly(string typeName)
    {
        var referencedAssemblies = Assembly.GetEntryAssembly()?
            .GetReferencedAssemblies()
            .Select(a => a.FullName);

        if (referencedAssemblies == null)
            return null;

        return AppDomain.CurrentDomain.GetAssemblies()
            .Where(a => referencedAssemblies.Contains(a.FullName))
            .SelectMany(a => a.GetTypes().Where(x => x.FullName == typeName || x.Name == typeName))
            .FirstOrDefault();
    }

    public static Type? GetFirstMatchingTypeFromCurrentDomainAssembly(string typeName) =>
        AppDomain.CurrentDomain.GetAssemblies()
            .SelectMany(a => a.GetTypes().Where(x => x.FullName == typeName || x.Name == typeName))
            .FirstOrDefault();
}
